package com.ckr.springcloud.controller;

import com.ckr.springcloud.pojo.Dept;
import com.ckr.springcloud.service.DeptService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Shadowckr
 * @create 2022-01-20 23:15
 */

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Resource
    private DeptService service;

    @Resource
    private DiscoveryClient client;

    // @RequestBody
    // 如果参数是放在请求体中，传入后台的话，那么后台要用@RequestBody才能接收到
    @PostMapping("/add")
    public boolean addDept(@RequestBody Dept dept) {
        return service.addDept(dept);
    }

    @GetMapping("/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return service.queryById(id);
    }

    @GetMapping("/list")
    public List<Dept> queryAll() {
        return service.queryAll();
    }

    //对于注册进eureka里面的微服务，可以通过服务发现来获得该服务的信息。【对外暴露服务】
    @GetMapping("/discovery")
    public Object discovery() {
        //获得微服务列表清单
        List<String> list = client.getServices();
        System.out.println("client.getServices()===>" + list);
        //得到一个具体的微服务！
        List<ServiceInstance> serviceInstanceList = client.getInstances("springcloud-provider-dept");
        for (ServiceInstance serviceInstance : serviceInstanceList) {
            System.out.println(
                    serviceInstance.getServiceId() + "\t" +
                            serviceInstance.getHost() + "\t" +
                            serviceInstance.getPort() + "\t" +
                            serviceInstance.getUri()
            );
        }
        return this.client;
    }
}
